package com.vng.zini.gifapp.utils;

import java.nio.ByteBuffer;
import java.util.ArrayList;

import com.vng.zini.gifapp.config.AppConstant;
import com.vng.zini.gifapp.config.AppParameters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;

public class VideoManager{
	public static String uri;
	public static Context context;
	public static int numScanFrames;
	public static int numIgnoreFrames ;
	public static void open(Context _context,String path){
		uri = path;
		context = _context;
		setParameterCamera();
	}
	
	public static ArrayList<Bitmap> extractVideo2BitmapList(int mode) {
		if(mode == AppConstant.EXTRACT_DEFAULT_MODE){
			return extractVideo2BitmapListDefault();
		}
		else if(mode == AppConstant.EXTRACT_FFMPEG_MODE){
			return convertVideo2BitmapList_FFmpeg();
		}
		return null;
	}
	
	private static ArrayList<Bitmap> extractVideo2BitmapListDefault() {
		ArrayList<Bitmap> lBitmap = new ArrayList<Bitmap>();
		try {
			long start = System.currentTimeMillis();
			// extract video to bitmap
			MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
			mRetriever.setDataSource(uri);
			for (int i = 0; i < 20; i++) {
				Bitmap bm = mRetriever.getFrameAtTime(330000 * i,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
				lBitmap.add(bm);

			}
			long stop = System.currentTimeMillis();
			System.out.println(stop - start);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return lBitmap;
	}
	
	private static ArrayList<Bitmap> convertVideo2BitmapList_FFmpeg() {
		ArrayList<Bitmap> lBitmap = new ArrayList<Bitmap>();
		try {
			long start = System.currentTimeMillis();
			FFmpeg ff = FFmpeg.getInstance();
			ff.openFile("/sdcard/myvideo.mp4");
			int width = ff.getWidth();
	        int height = ff.getHeight();
	        ff.setScreenSize(width,height);
	        
	        ff.prepareResources();
	        lBitmap = new ArrayList<Bitmap>();
	        
	        int new_width = AppConstant.GIF_STANDARD_WIDTH;
	        int new_height = AppConstant.GIF_STANDARD_HEIGHT;
	        for(int i=0;i<numScanFrames;i++){
	        	if(i%numIgnoreFrames != 0){
	        		ff.getNextDecodedFrame();
	        	}else{
			        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
			        
			        byte [] nativePixels = ff.getNextDecodedFrame();
			        ByteBuffer buffer = ByteBuffer.wrap(nativePixels);
			        bitmap.copyPixelsFromBuffer(buffer);
			        Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_8888,true);
			        Bitmap resized = getResizedBitmap(bitmap2,new_width,new_height);
			        lBitmap.add(resized);
	        	}
		        
	        }
			long stop = System.currentTimeMillis();
			System.out.println(stop - start);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return lBitmap;
	}
	
	private static void setParameterCamera(){
		switch(AppConstant.CAMERA_TYPE){
		case 0:
			numScanFrames = AppConstant.VIDEO_FEW_FRAMES;
			numIgnoreFrames = AppConstant.VIDEO_FEW_FRAMES / AppParameters.numberFrames;
			break;
		case 1:
			numScanFrames = AppConstant.VIDEO_MANY_FRAMES;
			numIgnoreFrames = AppConstant.VIDEO_MANY_FRAMES / AppParameters.numberFrames;
			break;
		}
		
	}
	
	private static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
		 
		int width = bm.getWidth();
		 
		int height = bm.getHeight();
		 
		float scaleWidth = ((float) newWidth) / width;
		 
		float scaleHeight = ((float) newHeight) / height;
		 
		// CREATE A MATRIX FOR THE MANIPULATION
		 
		Matrix matrix = new Matrix();
		 
		// RESIZE THE BIT MAP
		 
		matrix.postScale(scaleWidth, scaleHeight);
		 
		// RECREATE THE NEW BITMAP
		 
		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
		 
		return resizedBitmap;
		 
	}
}
